package com.itheima.consumer.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String message) throws Exception {
//        log.info("Spring消费者监听到的消息:"+message);
////        throw new Exception("我是故意发生错误的");
////        throw new MessageConversionException("我是故意的");
//        throw new RuntimeException("我是故意的");
//    }

    //下面这个可以接受到消息的id进行判断
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(Message message) throws Exception {
        log.info("Spring消费者监听到的消息:"+new String(message.getBody()));
        log.info("消息ID是:"+message.getMessageProperties().getMessageId());
//        throw new Exception("我是故意发生错误的");
//        throw new MessageConversionException("我是故意的");

    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueueMessage1(String message) throws InterruptedException {
        System.out.println("消费者1监听："+message);
        Thread.sleep(25);
    }


    @RabbitListener(queues = "work.queue")
    public void listenWorkQueueMessage2(String message) throws InterruptedException {
        System.err.println("消费者2监听："+message);
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message){
        log.info("消费者1监听到fanout.queue1的消息:"+message);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message){
        log.info("消费者2监听到fanout.queue2的消息:"+message);
    }

//    @RabbitListener(queues = "direct.queue1")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1",durable = "true"),
            exchange = @Exchange(name = "hmall.direct",type = "direct"),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String message){
        log.info("消费者1监听到direct.queue1的消息:"+message);
    }

//    @RabbitListener(queues = "direct.queue2")
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(name = "direct.queue2",durable = "true"),
        exchange = @Exchange(name = "hmall.direct",type = "direct"),
        key = {"red","yellow"}
))
    public void listenDirectQueue2(String message){
        log.info("消费者2监听到direct.queue2的消息:"+message);
    }

    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String message){
        log.info("消费者1监听到topic.queue1的消息:"+message);
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopic2Queue2(String message){
        log.info("消费者2监听到topic.queue2的消息:"+message);
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String,Object> message){
        log.info("消费者2监听到object.queue的消息:"+message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dlx.queue",durable = "true"),
            exchange = @Exchange(name = "dlx.direct",type = "direct"),
            key = {"hi"}
    ))
    public void listenDlxQueue(String message){
        log.info("消费者2监听到死信交换机的消息:"+message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue",durable = "true"),
            exchange = @Exchange(name = "delay.direct",delayed = "true"),
            key = {"hi"}
    ))
    public void listenDelayQueue(String message){
        log.info("消费者2监听到插件死信交换机的消息:"+message);
    }
}
